<?php
// +----------------------------------------------------------------------
// | HkCms 配置
// +----------------------------------------------------------------------
// | Copyright (c) 2012-2019 http://www.hkcms.cn, All rights reserved.
// +----------------------------------------------------------------------
// | Author: 广州恒企教育科技有限公司 <admin@hkcms.cn>
// +----------------------------------------------------------------------

namespace app\admin\controller;

use common\controller\AdminBase;
use think\Db;

class Config extends AdminBase
{
    private $Config = null;

    public function _initialize() 
    {

        parent::_initialize();

        $this->Config = new \common\model\Config;

        $configList = $this->Config->column("varname,value");

        $this->assign('Site', $configList);
    }

    public function index() 
    {

        if (  request()->isPost() ) {

            if ($this->Config->saveConfig($_POST)) {

                $this->success("更新成功！");

            } else {

                $error = $this->Config->getError();

                $this->error($error ? $error : "配置更新失败！");
            }

        }

        //首页模板
        $filepath = TEMPLATE_PATH . (empty(self::$Cache["Config"]['theme']) ? 'Default' : self::$Cache["Config"]['theme']) . '/content/index/';

        $indextp = str_replace($filepath, '', glob($filepath . 'index*'));

        $Urlrules = hk_cache('Urlrules');

        //URL规则
        $IndexURL = array();

        $TagURL = array();

        foreach ($Urlrules as $k => $v) {

            if ($v['file'] == 'tags') {

                $TagURL[$v['urlruleid']] = $v['example'];
            }

            if ($v['module'] == 'content' && $v['file'] == 'index') {

                $IndexURL[$v['ishtml']][$v['urlruleid']] = $v['example'];
            }
        }

        $this->assign('TagURL', $TagURL);
             
        $this->assign('IndexURL', $IndexURL);
             
        $this->assign('indextp', $indextp);
             
        return $this->fetch();
        
    }

    //邮箱参数
    public function mail() 
    {

        if ( request()->isPost() ) {

            return $this->index();
        }

        return $this->fetch();
    }

    //附件参数
    public function attach() 
    {

        if ( request()->isPost() ) {

            return $this->index();
        }

        $path = EXTEND_PATH . 'libs/driver/attachment/';

        $dirverList = glob($path . '*');

        $lang = array(

            'Local' => '本地存储驱动',

            'Ftp' => 'FTP远程附件驱动',
        );

        foreach ($dirverList as $k => $rs) {

            unset($dirverList[$k]);

            $dirverName = str_replace(array($path, '.php'), '', $rs);

            $dirverList[$dirverName] = $lang[$dirverName]? : $dirverName;
        }

        $this->assign('dirverList', $dirverList);

        return $this->fetch();
    }

    //高级配置
    public function addition() 
    {
        if ( request()->isPost() ) {

            if ($this->Config->addition($_POST)) {

                $this->success("修改成功，请及时更新缓存！");

            } else {

                $error = $this->Config->getError();

                $this->error($error ? $error : "高级配置更新失败！");
            }
        }
        $this->assign("addition", config());

        return $this->fetch();
    }

    //扩展配置
    public function extend() 
    {

        if ( request()->isPost() ) {

            $action = input('post.action');

            $input = input('post.');

            if ( isset($input['setting'])) {

                $setting = [

                    'title' => $input['setting']['title'],

                    'tips' => $input['setting']['tips'],

                    'style' => $input['setting']['style']
                ];
            }

            if ($action) {

                //添加扩展项
                if ($action == 'add') {

                    $data = [

                        'fieldname' => trim(input('post.fieldname')),

                        'type' => trim(input('post.type')),

                        'setting' => $setting,

                        // config("TOKEN_NAME") => input('post.' . config("TOKEN_NAME")),
                    ];

                    if ($this->Config->extendAdd($data) !== false) {

                        $this->success('扩展配置项添加成功！', url('Config/extend'));

                        return true;

                    } else {

                        $error = $this->Config->getError();

                        $this->error($error ? $error : '添加失败！');
                    }
                }

            } else {

                //更新扩展项配置
                if ($this->Config->saveExtendConfig($_POST)) {

                    $this->success("更新成功！");

                } else {

                    $error = $this->Config->getError();

                    $this->error($error ? $error : "配置更新失败！");
                }
            }
        }

        $action = input('action');

        $db = Db::name('ConfigField');

        if ($action) {

            // 如果是删除操作
            if ($action == 'delete') {

                $fid = input('fid', 0, 'intval');

                if ($this->Config->extendDel($fid)) {

                    // cache('Config', NULL);

                    $this->success("扩展配置项删除成功！");

                    return true;

                } else {

                    $error = $this->Config->getError();

                    $this->error($error ? $error : "扩展配置项删除失败！");
                }
            }
        }

        $extendList = $db->order(array('fid' => 'DESC'))->select();

        $this->assign('extendList', $extendList);

        return $this->fetch();
    }
}
